var pageComment;
$(function(){
   pageComment = 1;
   initShow();
   // getComment()
   dianZan1();
   getFocus();
});

/**
 * 初始化评论页面
 */
function initShow(){
    var articleId=$(".content-wrap .content .article-header").find("[id=articleId]").val();
    var userId = $("#hId").val();
    var getReplyAjax = $.ajax({
        url:"getReply",
        type:"GET",
        data:{
            "articleId":articleId,
            "page":pageComment,
            "userId":userId
        }
    });
    getReplyAjax.done(function (result) {
        console.log(result.extend.replies)
        if (result.code == 100){
            var replyLt = result.extend.replyList;
            var list = result.extend.replies;
            var s = "";
            $.each(replyLt,function(i,reply) {
                s += cloneCommentByReader(this);
                $.each(list,function(i,list1){
                    $.each(this,function(){
                        if (reply.replyId == this.commentId){
                            $.each(list1,function () {
                                s+= cloneReply(this);
                            })
                        }
                    })
                })
            });
            $(".comment-show").append(s);
            var replyClone ="<div class='alert alert-info text-center' id='moreComment' onclick='moreComment()'>加载更多评论</div>";
            $(".comment-show").append(replyClone);
        } else{
            var replyClone ="<div class='alert alert-info text-center' id='moreComment'>没有评论了</div>";
            $(".comment-show").append(replyClone);
        }
        commentDianZan();
    })
}
/**
 * 评论按钮事件
 * @param obj
 */
function comment(obj){
    var articleid=$(".content-wrap .content .article-header").find("[id=articleId]").val();
    var content=$(obj).prev();
    var userIdByArticle = $("#userIdByArticle").val();
    var userId = $("#hId").val();
    if (userIdByArticle == userId){
        layer.msg("自己不能给自己评论",{offset:'300px' });
    }else {
        if(content.val()==""){
            layer.msg("请填写评论内容",{offset:'300px' });
            return;
        }
        $.post("insertComment",{"articleid":articleid,"content":content.val(),"userIdByArticle":userIdByArticle},function(result){
            var obj=result.extend.reply;
            var cloneCtByReader = cloneCommentByReader(obj);
            $(".comment-show").append(cloneCtByReader);
            layer.msg("评论成功",{offset:'300px' });
        });
    }

}

/**
 * 获取更多评论   分页操作
 */
function moreComment(){
   $("#moreComment").remove();
   pageComment++;
   initShow();
}

/**
 * 删除评论
 * @param obj
 */
function  deleteComment(obj){

    var replyId = $("#replyId").val();
    var deleteCt = $.ajax({
        url : "deleteComment",
        type : "get",
        data : {
            "replyId":replyId
        }
    });
    deleteCt.done(function (result){
        if (result.code == 100){
            $(obj).parents(".comment-show-con").remove();
        } else{
            layer.msg("删除失败",{offset:'300px' });
        }
    })
}

/**
 * 关注信息
 */
$("#focus").click(function () {
    var userId = $("#hId").val();
    var focusId = $("#userIdByArticle").val();
    if (userId == focusId) {
        layer.msg("你这么自恋不好吧",{offset:'300px' });
    } else {
        var at = $.ajax({
            url: "focus",
            type: "get",
            data: {
                "userId": userId,
                "focusId": focusId
            }
        })
        at.done(function (result) {
            console.log(result)
            if (result.code == 100) {
                $("#focus").text('已关注');
            } else {
                layer.msg(result.extend.msg,{offset:'300px' });
            }
        })
    }
})

/**
 * 获取是否关注
 */
function getFocus(){
    var userId = $("#hId").val();
    var focusId = $("#userIdByArticle").val();
    var fs = $.ajax({
        url : "isFocus",
        type : "get",
        data : {
            "userId":userId,
            "focusId":focusId
        }
    })
    fs.done(function(result){
        if (result.code == 100) {
            $("#focus").text('已关注');
        }
    })
}
